package com.myWeb.dao;

import com.myWeb.entity.Users;
import com.myWeb.util.JdbcUtil;

import javax.xml.transform.Result;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletRequest;

public class UserDao {

    private JdbcUtil util = new JdbcUtil();

    //增加用户信息
    public int add(Users user) {
        String sql = "insert into users(userName,password,sex,email)" + "values(?,?,?,?)";

        int result = 0;
        Connection conn = null;
        PreparedStatement ps = null;
        try {
            conn = util.getConnection();
            ps = conn.prepareStatement(sql);
            ps.setString(1, user.getUserName());
            ps.setString(2, user.getPassword());
            ps.setString(3, user.getSex());
            ps.setString(4, user.getEmail());
            result = ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            util.close(null, ps, conn);
        }
        return result;
    }

    public int add(Users user, HttpServletRequest request) {
        String sql = "insert into users(userName,password,sex,email)" + "values(?,?,?,?)";
        int result = 0;
        PreparedStatement ps ;
        try {
            ps = util.createStatement(sql,request);
            ps.setString(1, user.getUserName());
            ps.setString(2, user.getPassword());
            ps.setString(3, user.getSex());
            ps.setString(4, user.getEmail());
            result = ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            util.close(request);
        }
        return result;
    }

    //查询所有用户查询信息
    public List findAll() {
        String sql = "select * from users";
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        List userList = new ArrayList();
        try {
            conn = util.getConnection();
            ps = conn.prepareStatement(sql);
            rs = ps.executeQuery();
            while (rs.next()) {
                Integer userId = rs.getInt("userId");
                String userName = rs.getString("userName");
                String password = rs.getString("password");
                String sex = rs.getString("sex");
                String email = rs.getString("email");
                Users users = new Users(userId, userName, password, sex, email);
                userList.add(users);

            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            util.close(rs, ps, conn);
        }
        return userList;
    }

    //登录验证
    public int login(String userName, String password) {
        String sql = "select count(*) from users where userName=? and password=? ";
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        int result = 0;
        try {
            conn = util.getConnection();
            ps = conn.prepareStatement(sql);
            ps.setString(1, userName);
            ps.setString(2, password);
            rs = ps.executeQuery();
            while(rs.next()){
                result = rs.getInt("count(*)");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            util.close(rs, ps, conn);
        }
        return result;
    }

    //根据用户编号删除用户信息
    public int delete(String userId) {
        String sql = "delete from users where userId=?";
        Connection conn = null;
        PreparedStatement ps = null;
        int result = 0;
        try {
            conn = util.getConnection();
            ps = conn.prepareStatement(sql);
            ps.setInt(1,Integer.valueOf(userId));
            result = ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            util.close(null, ps, conn);
        }
        return result;
    }
}








